﻿using System;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using System.Windows.Input;

namespace SwAssem
{
    partial class MainWindow
    {
        #region 树节点右键效果样式
        /// <summary>
        /// 右键高亮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }
        }
        /// <summary>
        /// 右键高亮
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }
        #endregion

        #region 初始化
        private void Init()
        {
            this.AssemTree.SelectedItemChanged += OnTreeViewItemSelectedChange;
            this.Panle2_Btn1.Click += OpenSelPartDlg;
            this.Panle3_Btn1.Click += OpenSelPartDlg;

            ModelDoc2 modelDoc = Swapp.ActiveDoc;
            AssemblyDoc assemblyDoc = modelDoc as AssemblyDoc;
            if(assemblyDoc == null)
            {
                MessageBox.Show("只允许在装配下使用");
                this.Close();
                return;
            }
            string rootPath = modelDoc.GetPathName();
            string rootnodename = Path.GetFileNameWithoutExtension(rootPath);
            string rootnodenamewithext = Path.GetFileName(rootPath);

            modelDoc.ClearSelection2(true);
            bool boolstatus = modelDoc.Extension.SelectByID2(rootnodenamewithext, "COMPONENT", 0, 0, 0, false, 0, null, 0);
            Component2 rootComponent = modelDoc.ISelectionManager.GetSelectedObject(1);
            modelDoc.ClearSelection2(true);

            TreeViewItem rootNodeItem = new TreeViewItem();
            rootNodeItem.Header = rootnodename;
            rootNodeItem.Tag = rootComponent;
            rootNodeItem.IsExpanded = true;
            AssemTree.Items.Add(rootNodeItem);

            object[] components = assemblyDoc.GetComponents(true);
            foreach(Component2 _con in  components)
            {
                TreeViewItem item1 = new TreeViewItem();
                item1.Header = _con.Name2;
                item1.Tag = _con;
                rootNodeItem.Items.Add(item1);
                getChildComponent(_con, item1);
            }

            //设置顶层透明度
            rootNodeItem.IsSelected = true;
            rootNodeItem.Focus();
            boolstatus = assemblyDoc.SetComponentTransparent(true);
            modelDoc.ClearSelection();

            modelDoc.Rebuild((int)swRebuildOptions_e.swRebuildAll);
        }
        private void getChildComponent(Component2 parentCom,TreeViewItem parentItem)
        {
            parentItem.MouseRightButtonUp += OnTreeViewItemMouse_RightUp;

            foreach(Component2 _con in parentCom.GetChildren())
            {
                TreeViewItem item1 = new TreeViewItem();
                item1.Header = _con.Name2;
                item1.Tag = _con;
                parentItem.Items.Add(item1);
                getChildComponent(_con, item1);
            }
        }
        #endregion

        #region 注册类事件
        private void OnTreeViewItemMouse_RightUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu contextmenu = new ContextMenu();
            MenuItem menuItem1 = new MenuItem();
            menuItem1.Name = "ADD1";
            menuItem1.Header = "添加到装配对象一";
            menuItem1.Click += OnTreeMenuItemClick;

            MenuItem menuItem2 = new MenuItem();
            menuItem2.Name = "ADD2";
            menuItem2.Header = "添加到装配对象二";
            menuItem2.Click += OnTreeMenuItemClick;
            contextmenu.Items.Add(menuItem1);
            contextmenu.Items.Add(menuItem2);

            TreeViewItem selItem = AssemTree.SelectedItem as TreeViewItem;
            if(selItem != null && selItem.Tag != null)
            {
                selItem.ContextMenu = contextmenu;
            }
        }


        #endregion
    }
}
